<template>
  <div class="setting-container">
    <el-card class="box-card">
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <!-- 改变底部线的长短问题 -->
          <template v-slot:label>
            <span> 角色管理</span>
          </template>
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="enterEdit"
          >新增角色
          </el-button>
          <!-- table表格 -->
          <el-table v-loading="loading" :data="tableData" style="width: 100%">
            <el-table-column
              prop="date"
              type="index"
              label="序号"
              width="180"
            />
            <el-table-column prop="name" sortable label="角色名" width="180" />1
            <el-table-column prop="description" sortable label="描述" />
            <el-table-column prop="address" sortable width="300" label="操作">
              <template v-slot:default="{ row }">
                <el-button type="text">分配权限</el-button>
                <el-button type="text" @click="getEdit(row.id)">修改</el-button>
                <!-- 删除 -->
                <el-popconfirm
                  title="这是一段内容确定删除吗？"
                  @onConfirm="delBtnss(row.id)"
                >
                  <el-button slot="reference" type="text">删除</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-pagination
            :current-page.sync="currentPage4"
            :page-size.sync="pageSize"
            layout="total, prev, pager, next"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <!-- 改变底部线的长短问题 -->
          <template v-slot:label>
            <span> 角色管理</span>
          </template>
          <settings />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 弹框 -->
    <settingDloag ref="dalog" />
  </div>
</template>

<script>
import { delRoleById, getRoleById, getRoles } from '../../api/setting'
// 导入组件
import settings from '../../views/setting/components/index.vue'
import settingDloag from '../../views/setting/components/settingDialog.vue'
export default {
  name: 'Setting',
  components: {
    settings,
    settingDloag
  },
  data() {
    return {
      tableData: [],
      // 页码
      currentPage4: 1,
      // 页容量选项
      // pageSizes: [2, 4, 6, 9],
      // 当前的页容量
      pageSize: 2,
      // 总条数
      total: 21,
      activeName: 'first',
      loading: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // pop删除
    async delBtnss(id) {
      console.log('111')
      await delRoleById(id)
      this.$message.success('删除成功')
      if (this.tableData.length === 1 && this.currentPage4 > 1) {
        this.currentPage4--
      }
      this.getData()
    },
    // 编辑角色按钮
    async getEdit(id) {
      console.log(id)
      const ress = await getRoleById(id)
      console.log('ress:', ress)
      this.$refs.dalog.form = ress
      this.$refs.dalog.visibles = true
    },
    // 新增角色按钮
    enterEdit() {
      console.log('111')
      this.$refs.dalog.visibles = true
    },
    // 获取数据
    async getData() {
      this.loading = true
      const res = await getRoles({
        page: this.currentPage4,
        pagesize: this.pageSize
      })
      console.log('res:', res)
      // 保存数据
      this.tableData = res.rows
      // 保存总条数
      this.total = res.total
      setTimeout(() => {
        this.loading = false
      }, 500)
    },
    // // 页容量
    // // val 最新的页容量(一页多少条)
    // handleSizeChange(val) {
    //   console.log(`每页 ${val} 条`)
    // },
    // 页码改变
    // val 当前页码
    handleCurrentChange(val) {
      this.getData()
      console.log(`当前页: ${val}`)
    }
  }
}
</script>

<style lang="scss">
.setting-container {
  .box-card {
    padding: 15px;
    background: #f0f2f5;
  }
  .el-tabs__item {
    font-size: 18px;
    span {
      padding: 0 20px;
    }
  }
  .el-table__header-wrapper {
    background: #fafafa;
  }
  .el-pagination {
    text-align: right;
  }
}
</style>
